package com.example.addemoone.kotlin

fun main() {
    val person = Person("", 18)
    person.intro()
    val person1 = Person1("张三", -18)
    person1.intro()
}

/**
 * 主构造函数
 */

// 1.显式主构造函数
class Person constructor(var name: String, var age: Int) {
    init {
        println("初始化")
        require(name.isNotEmpty(), {"姓名不能为空"})
        require(age > 0, {"年龄不能小于0"})
    }

    fun intro() {
        println("我叫$name,今年${age}岁")
    }
}

// 2.隐式主构造函数
class Person1(var name: String, var age: Int) {
    init {
        println("初始化")
        require(name.isNotEmpty(), {"姓名不能为空"})
        require(age > 0, {"年龄不能小于0"})
    }

    fun intro() {
        println("我叫$name,今年${age}岁")
    }
}

